package controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.UrlUtility;

import bizlogic.PriorityManager;
import model.PriorityDTO;
import model.error.PriorityErrorDTO;

@WebServlet("/ProcessPriority")
public class ProcessPriorityServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ProcessPriorityServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doShow(request, response);
	}
	
	private void doShow(HttpServletRequest req, HttpServletResponse res) {
		String address = "/WEB-INF/ManagePriority.jsp";
		String action = req.getParameter("action");
		
		PriorityDTO data = null;
		PriorityManager pm = new PriorityManager();
		
		if ( action.trim().equalsIgnoreCase("new") ) {
			data = new PriorityDTO();
			
			req.setAttribute("action", "new");
			req.setAttribute("data", data);
			
			UrlUtility.forwardPage(req, res, address);
		} else if ( action.trim().equalsIgnoreCase("edit") ) {
			boolean valid = true;
			int code;
			
			try {
				code = Integer.parseInt(req.getParameter("code"));
				
				data = pm.getPriority(code);
				if ( data == null ) valid = false;
			} catch ( NumberFormatException e ) {
				valid = false;
			}
			
			if ( valid ) {
				req.setAttribute("action", "edit");
				req.setAttribute("data", data);
				
				UrlUtility.forwardPage(req, res, address);
			} else {
				UrlUtility.redirectPage(res, "LoadPriority");
			}
		} else {
			UrlUtility.redirectPage(res, "LoadPriority");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}
	
	private PriorityDTO validate(HttpServletRequest request,HttpServletResponse response) {
		PriorityDTO data = new PriorityDTO();
		PriorityErrorDTO error = new PriorityErrorDTO();
		
		// Validate Code
		if ( request.getParameter("code") == null || request.getParameter("code").trim().equals("") ) {
			error.setValid(false);
			error.setErrorCode("priority.error.coderequired");
		} else {
			try {
				data.setCode(Integer.parseInt(request.getParameter("code")));
			} catch ( NumberFormatException e ) {
				error.setValid(false);
				error.setErrorCode("priority.error.invalidnumbercode");
			}
		}
		
		if ( request.getParameter("level") == null || request.getParameter("level").trim().equalsIgnoreCase("") ) {
			error.setValid(false);
			error.setErrorLevel("priority.error.levelrequired");
		} else {
			data.setLevel(request.getParameter("level"));
		}
		
		request.setAttribute("error", error);
		
		return data;
	}
	
	private void processRequest(HttpServletRequest request,HttpServletResponse response)
	{
		PriorityManager pm = new PriorityManager();
		PriorityDTO pDTO = new PriorityDTO();
		PriorityErrorDTO err = null;
		
		String action = "";
		
		// Validation
		pDTO = validate(request, response);
		err = (PriorityErrorDTO)request.getAttribute("error");
		
		if ( err.isValid() ) {
			action = request.getParameter("action");
			
			if ( action.trim().equalsIgnoreCase("new") ) {
				err.setErrorGeneral(pm.insertPriority(pDTO));
			} else if ( action.trim().equalsIgnoreCase("edit") ) {
				err.setErrorGeneral(pm.updatePriority(pDTO));
			}
			
			if ( err.getErrorGeneral() != null && !err.getErrorGeneral().trim().equals("") )
				err.setValid(false);
		}
		
		if ( err.isValid() ) {
			UrlUtility.redirectPage(response, "LoadPriority");
		} else {
			request.setAttribute("action", action);
			request.setAttribute("error", err);
			request.setAttribute("data", pDTO);
			
			UrlUtility.forwardPage(request, response, "/WEB-INF/ManagePriority.jsp");
		}
	}

}
